home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-07-18 | 3.5 KB | 137 lines |
- /*
- * @(#)ISAPIResponse.java 1.11 97/07/16
- *
- * Copyright (c) 1995-1997 Sun Microsystems, Inc. All Rights Reserved.
- *
- * This software is the confidential and proprietary information of Sun
- * Microsystems, Inc. ("Confidential Information"). You shall not
- * disclose such Confidential Information and shall use it only in
- * accordance with the terms of the license agreement you entered into
- * with Sun.
- *
- * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
- * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
- * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
- * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
- * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
- * THIS SOFTWARE OR ITS DERIVATIVES.
- *
- * CopyrightVersion 1.0
- */
-
- package sun.servlet.isapi;
-
- import javax.servlet.ServletOutputStream;
- import sun.servlet.http.HttpResponse;
- import java.io.IOException;
-
- /**
- * This class represents a servlet response in the ISAPI extension version
- * of the servlet runner.
- *
- * @version 1.11, 07/16/97
- * @author David Connelly
- * @author Jongyoon Lee
- */
- class ISAPIResponse extends HttpResponse {
- /*
- * The ISAPIConnection for this response
- */
- private ISAPIConnection conn;
-
- ISAPIResponse() {
- this(null);
- }
-
- ISAPIResponse(ISAPIConnection conn) {
- super();
- out = new ISAPIOutputStream();
- init(conn);
- }
-
- public void init(ISAPIConnection conn) {
- this.conn = conn;
- ((ISAPIOutputStream)out).init(conn);
- out.setObserver(this);
- keepAlive = false;
- }
-
- /**
- * Returns an output stream for writing response data.
- */
- public ServletOutputStream getOutputStream() {
- return out;
- }
-
- /**
- * Sends an error response to the client using the specified status
- * code and detail message.
- * @param sc the status code
- * @param msg the detail message
- * @exception IOException If an I/O error has occurred.
- */
- public void sendError(int sc, String msg) throws IOException {
- ISAPIOutputStream out = (ISAPIOutputStream)this.out;
- if (!out.isCommitted()) {
- headers.clear();
- out.next();
- out.setObserver(this);
- out.setIOException(null);
- setStatus(sc);
- setContentType("text/html");
- String title = status + " " + reason;
- out.print("<head><title>");
- out.print("Servlet Runner");
- out.println("</title></head>");
- out.print("<h1>");
- out.print(title);
- out.println("</h1><body>");
- if (msg != null) {
- out.print(msg);
- out.println("<p>");
- }
- out.println("</body>");
- out.finish();
- }
- }
-
- /*
- * Writes status line and headers to specified servlet output stream.
- */
- protected void writeHeaders() throws IOException {
- bufHeader.reset();
- outHeader.next();
- outHeader.init(bufHeader);
- int len;
- if ((len = out.getContentLength()) != -1) {
- if (!contentLenSet) {
- headers.putIntHeader("Content-Length", len);
- }
- }
- headers.write(outHeader);
- outHeader.flush();
- if (status == 0) {
- status = SC_OK;
- reason = reason(SC_OK);
- }
- byte[] buf = bufHeader.toByteArray();
- headerBytes = buf.length;
- String stat = new String(status + " " + reason);
- byte[] statbuf = stat.getBytes();
- conn.writeHeaders(statbuf, 0, statbuf.length, buf, 0, buf.length);
- }
-
- /**
- * Finishes the response.
- */
- public void finish() throws IOException {
- if (out != null) {
- out.finish();
- }
- if (conn != null) {
- conn.finish();
- conn = null;
- }
- }
- }
-